package com.financing.platform.web.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.FriendshipLinkDTO;
import com.financing.platform.service.IFriendshipLinkService;
import com.financing.platform.util.DTOdateSet;

@Controller("friendshipLinkActionApi")
@RequestMapping("/api/fl")
@Scope("prototype")
public class FriendshipLinkAction extends BaseAction{
	
	@Autowired
	IFriendshipLinkService friendshipLinkService;
	
	@RequestMapping("/queryList.shtml")
	@ResponseBody
	public ResultMap queryList(HttpServletRequest request,
			HttpServletResponse response,FriendshipLinkDTO friendshipLinkDTO) {
		try{
			log.info("Begin query fa user list!");
			List<FriendshipLinkDTO> faUserList = friendshipLinkService.queryList(friendshipLinkDTO);
			
			resultMap.setAttribute("list",faUserList);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
			log.info("End query fa user list!");
		}catch(Exception e){
			log.error("queryFaUserList error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
			
			return resultMap;
	}
	
	@RequestMapping("/addFl.shtml")
	@ResponseBody
	public ResultMap add(HttpServletRequest request,
			HttpServletResponse response,FriendshipLinkDTO friendshipLinkDTO) {
		try{
			log.info("Begin add fa user info !");
			
			DTOdateSet.setCreateInfo(friendshipLinkDTO, request.getSession());
			
			Integer id =friendshipLinkService.add(friendshipLinkDTO);
			
			resultMap.getAttribute().put("id",id);
			
			if(id !=null){
			    resultMap.setStatus(STATUS.SUCCESS);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			}else{
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
			}
			log.info("End add fa user info !");
		}catch(Exception e){
			log.error("update fa user info error!",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
			return resultMap;
	}
	
	@RequestMapping("/updateFl.shtml")
	@ResponseBody
	public ResultMap update(HttpServletRequest request,
			HttpServletResponse response,FriendshipLinkDTO friendshipLinkDTO) {
		try{
			log.info("Begin update fa user info !");
			
			DTOdateSet.setUpdateInfo(friendshipLinkDTO, request.getSession());
			
			int count = friendshipLinkService.update(friendshipLinkDTO);
			
			resultMap.getAttribute().put("update count",count);
			if(count >0){
			    resultMap.setStatus(STATUS.SUCCESS);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			}else{
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
			}
			log.info("End update fa user info !");
		}catch(Exception e){
			log.error("update fa user info error!",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
			return resultMap;
				
	}
	
	@RequestMapping("/deleteFl.shtml")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request,
			HttpServletResponse response,FriendshipLinkDTO friendshipLinkDTO) {
		try{
			log.info("Begin delete fa user info !");
			int count = friendshipLinkService.delete(friendshipLinkDTO.getId());
			
			resultMap.getAttribute().put("delete count",count);
			
			if(count > 0){
			    resultMap.setStatus(STATUS.SUCCESS);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			}else{
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
			}
			log.info("End delete fa user info !");
		}catch(Exception e){
			log.error("delete fa user info error!",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
			return resultMap;
				
	}
	
	
	@RequestMapping("/fLDetail.shtml")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request,
			HttpServletResponse response, FriendshipLinkDTO friendshipLinkDTO) {
		try{
			FriendshipLinkDTO dto = friendshipLinkService.get(friendshipLinkDTO.getId());
			
			resultMap.setAttribute("FLDetail", dto);
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("query friendshipLink list error",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		
		return resultMap;

	}
	
}
